//
//  UIView+AFF.m
//  AnyfishApp
//
//  Created by Bob Lee on 15/11/20.
//  Copyright © 2015年 Anyfish. All rights reserved.
//

#import "UIView+AFF.h"

static void * codeKey = (void *)@"viewDataKey";
static void * textKey = (void *)@"DataTextKey";
static void * textImgKey = (void *)@"DataTextImgKey";
static void * codeAutoPull = (void *)@"AutoPull";
static void * loadSign = (void *)@"loadSign";

@implementation UIView (AFF)
/**
 * xib创建的view
 */
+ (instancetype)mh_viewFromXib
{
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
}


- (AFFData*)aData {
    return objc_getAssociatedObject(self, codeKey);
}

- (void)setAData:(AFFData *)aData {
    objc_setAssociatedObject(self, codeKey, aData, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}




- (BOOL)isLoading {
    return ((NSNumber*)objc_getAssociatedObject(self, loadSign)).boolValue;
}

- (void)setIsLoading:(BOOL)isLoading {
    objc_setAssociatedObject(self, loadSign, @(isLoading), OBJC_ASSOCIATION_ASSIGN);
}

- (void)removeAllSubView {
    for(UIView *view in self.subviews){
        [view removeFromSuperview];
    }
}

- (void)removeAllSubLayer {
    if (self.layer.sublayers.count>0) {
        for(NSInteger i=self.layer.sublayers.count-1; i>=0; i--){
            CALayer *layer = [self.layer.sublayers objectWithIndex:i];
            if(layer){
                [layer removeFromSuperlayer];
            }
        }
    }
}

- (void)removeSubViewWithClass:(Class)cls {
    for(UIView *view in self.subviews){
        if([view isKindOfClass:cls]){
            [view removeFromSuperview];
        }
    }
}

- (void)removeSubViewWithTag:(NSInteger)tag {
    for(UIView *view in self.subviews){
        if(view.tag == tag)
            [view removeFromSuperview];
    }
}

#pragma mark - 设置部分圆角
/**
 *  设置部分圆角(绝对布局)
 *
 *  @param corners 需要设置为圆角的角 UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerBottomRight | UIRectCornerAllCorners
 *  @param radii   需要设置的圆角大小 例如 CGSizeMake(20.0f, 20.0f)
 */
- (void)addRoundedCorners:(UIRectCorner)corners
                withRadii:(CGSize)radii {
    
    UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:radii];
    CAShapeLayer* shape = [[CAShapeLayer alloc] init];
    [shape setPath:rounded.CGPath];
    
    self.layer.mask = shape;
}

/**
 *  设置部分圆角(相对布局)
 *
 *  @param corners 需要设置为圆角的角 UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerBottomRight | UIRectCornerAllCorners
 *  @param radii   需要设置的圆角大小 例如 CGSizeMake(20.0f, 20.0f)
 *  @param rect    需要设置的圆角view的rect
 */
- (void)addRoundedCorners:(UIRectCorner)corners
                withRadii:(CGSize)radii
                 viewRect:(CGRect)rect {
    
    UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:radii];
    CAShapeLayer* shape = [[CAShapeLayer alloc] init];
    [shape setPath:rounded.CGPath];
    
    self.layer.mask = shape;
}

+ (UIView *)progressViewWithFrame:(CGRect)frame
                          persent:(CGFloat)persent
                          bgColor:(UIColor *)bgColor
                         proColor:(UIColor *)proColor
                           cornor:(CGFloat)cornor
                           direct:(BOOL)isHorizontal {
    UIView *view = [[UIView alloc] initWithFrame:frame];
    view.backgroundColor = bgColor;
    view.layer.cornerRadius = cornor;
    view.layer.masksToBounds = YES;
    
    CALayer *layer = [CALayer layer];
    CGRect fr = CGRectZero;
    fr.size = frame.size;
    if (isHorizontal) {
        fr.size.width = frame.size.width * persent;
    } else {
        fr.size.height = frame.size.height * persent;
    }
    layer.frame = fr;
    layer.cornerRadius = cornor;
    layer.backgroundColor = proColor.CGColor;
    [view.layer addSublayer:layer];
    
    return view;
}

@end
